/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.imr.support; 

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;

/**
 *
 * @author macbook
 */
@SuppressWarnings("unchecked")
public class Dao <T extends Serializable> {
 
    private final Session session; 
    private final Class<T> persistentClass;
    
	/**
	 * 
	 */
	public Dao() {
        this.session = HibernateUtil.getSession();
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    /**
     * @return
     */
	protected Session getSession() {
        return session;
    } 
    
    /**
     * @param entity
     * @throws Exception
     */
    public void save(T entity) throws Exception {
        try {
        	session.getTransaction().begin();
        	session.saveOrUpdate(entity);
        	session.getTransaction().commit();
        } catch (Throwable t) {
        	session.getTransaction().rollback();
            t.printStackTrace();
            throw new Exception(t);
        } finally {
            close();
        }
    }
    
    /**
     * @param entity
     * @throws Exception
     */
    public void update(T entity) throws Exception { 
        try {
        	session.getTransaction().begin();
        	session.update(entity);
        	session.getTransaction().commit();
        } catch (Throwable t) {
        	session.getTransaction().rollback(); 
            t.printStackTrace(); 
            throw new Exception(t);
        } finally { 
            close(); 
        } 
    }

    /**
     * @param entity
     * @throws Exception
     */
    public void remove(T entity) throws Exception {
        try {
        	session.getTransaction().begin();
        	session.delete(entity);
        	session.getTransaction().commit();
        } catch (Throwable t) {
        	session.getTransaction().rollback();
            t.printStackTrace();
            throw new Exception(t);
        } finally { 
            close(); 
        } 
    } 
    
    /**
     * @return
     */
    public List<T> findAll(){
    	List<T> list = new ArrayList<T>();
        try {
        	list = getSession().createCriteria(persistentClass).list(); 
        } catch (Throwable t) { 
            t.printStackTrace(); 
        } finally { 
            close(); 
        } 
        return list;
    } 
     
    /**
     * 
     */
    private void close() {
        if (getSession() != null && getSession().isOpen()) {
            getSession().close();
        }
    }   
    
    /**
     * @return
     */
    protected Criteria createCriteria(){
        return getSession().createCriteria(persistentClass);
    }    
    
    /**
     * @param nome
     * @return
     */
    protected Criteria createCriteria(String nome){  
        return getSession().createCriteria(persistentClass,nome);
    }    
    
    /**
     * @param criteria
     * @return
     */
    protected List<T> findByCriteria(Criteria criteria){
    	List<T> list = criteria.list(); 
        close();
    	return list;
    }
    
    /**
     * @param value
     * @return
     */
    protected boolean isNotNull(Long value) {
    	boolean set = false;
		 if(value != null && value.intValue() > 0) {
			 set = true;
		 }
		 return set;
	}

    /**
     * @param value
     * @return
     */
    protected boolean isNotNull(Character value) {
    	boolean set = false;
		 if(value != null) {
			 set = true;
		 }
		 return set;
	}
    
    /**
     * @param value
     * @return
     */
    protected boolean isNotNull(String value) {
    	boolean set = false;
		 if(value != null && !"".equals(value.trim())) {
			 set = true;
		 }
		 return set;
	}

    /**
     * @param value
     * @return
     */
    protected boolean isNotNull(Date value) {
    	boolean set = false;
		 if(value != null) {
			 set = true;
		 }
		 return set;
	}
    
    /**
     * @param value
     * @return
     */
    protected boolean isNotNull(Boolean value) {
    	boolean set = false;
    	if(value != null) {
    		set = true;
    	}
    	return set;
    }
}
